//
//  SXSearchBar.swift
//  sina
//
//  Created by shengxinwei on 16/5/19.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit

class SXSearchBar: UIView {

    @IBOutlet weak var searchTF: UITextField!
 
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var rightConstraint: NSLayoutConstraint!
    
    
    class func loadNibView()->SXSearchBar {
        return NSBundle.mainBundle().loadNibNamed("SXSearchBar", owner: nil, options: nil).last as! SXSearchBar
    }
    
    //在  awakeFromNib 做初始设置
    override func awakeFromNib() {
        //设置宽度
        //bounds.width中的width知识bounds的只读属性
        //要修改大小需要通过size进行修改
        self.bounds.size.width = ScreenWidth
        //文本输入框有leftView
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        //设置显示模式
        searchTF.leftViewMode = .Always
        //修改frame
        searchTF.leftView?.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
        //设置视图的显示模式
        searchTF.leftView?.contentMode = .Center
        
        
        //设置圆角
        //        self.layer.cornerRadius = 20
        //        self.layer.masksToBounds = true
        //设置边线的颜色
        //        self.layer.borderColor = UIColor.orangeColor().CGColor
        //        self.layer.borderWidth = 2
    }
    @IBAction func textFieldBeginEditing(sender: AnyObject) {
        rightConstraint.constant = cancelBtn.bounds.width
        
        UIView.animateWithDuration(0.25) { 
            self.layoutIfNeeded(); //提前调用layoutsubviews
        }
    }
    @IBAction func textFieldEndEditing(sender: UITextField) {
        rightConstraint.constant = 0
        UIView.animateWithDuration(0.25) {
            self.layoutIfNeeded(); //提前调用layoutsubviews
        }
    }
    
    @IBAction func cancelBtnDidClick(sender: AnyObject) {
        self.endEditing(true)
    }
    
}
